﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class ColorPickerControl : UserControl, IControlRulable
    {
        private ColorPicker innerColorPicker;

        public ColorPicker InnerColorPicker
        {
            get { return innerColorPicker; }
            set { innerColorPicker = value; }
        }

        public ColorPickerControl()
        {
            InitializeComponent();
            this.innerColorPicker = new ColorPicker();
            this.FillControls();
        }

        public ColorPickerControl(ColorPicker picker):this()
        {
            this.innerColorPicker = picker;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcCommand.Value = this.innerColorPicker.Command;
                this.rcCommandPreview.Value = this.innerColorPicker.CommandPreview;
                this.rcCommandRevert.Value = this.innerColorPicker.CommandRevert;
                this.strId.Value = this.innerColorPicker.Id;
                this.strQueryCommand.Value = this.innerColorPicker.QueryCommand;
                this.icSequence.Value = this.innerColorPicker.Sequence;
                this.strTemplateAlias.Value = this.innerColorPicker.TemplateAlias;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerColorPicker.Command = this.rcCommand.Value;
                    this.innerColorPicker.CommandPreview = this.rcCommandPreview.Value;
                    this.innerColorPicker.CommandRevert = this.rcCommandRevert.Value;
                    this.innerColorPicker.Id = this.strId.Value;
                    this.innerColorPicker.QueryCommand = this.strQueryCommand.Value;
                    this.innerColorPicker.Sequence = this.icSequence.Value;
                    this.innerColorPicker.TemplateAlias = this.strTemplateAlias.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ColorPickerControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerColorPicker.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
